{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python369jvsc74a57bd054f0cca124f38538f0af9da5035feb8330e039430efda7f6532c7bec2bbfbd8a",
   "display_name": "Python 3.6.9 64-bit ('pysyft': virtualenvwrapper)"
  },
  "metadata": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入软件包\n",
    "\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "\n",
    "import random\n",
    "import csv\n",
    "import pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2    3904\n",
       "3    2546\n",
       "1    2100\n",
       "4    1795\n",
       "0    1253\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "# 1 读取数据\n",
    "\n",
    "dataframe = pandas.read_csv(\"../data/maldroid2020/feature_vectors_syscallsbinders_frequency_5_Cat.csv\")\n",
    "array = dataframe.values\n",
    "from sklearn.utils import shuffle\n",
    "array = shuffle(array)\n",
    "\n",
    "# random.shuffle(array) # random the dataset\n",
    "features = array[:,0:470]\n",
    "labels = array[:,470]-1\n",
    "pandas.value_counts(labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2 处理数据\n",
    "from sklearn import preprocessing\n",
    "min_max_scaler = preprocessing.MinMaxScaler()\n",
    "features = min_max_scaler.fit_transform(features)\n",
    "features = torch.FloatTensor(features)\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train_features,test_features, train_labels, test_labels = train_test_split(features,labels,  test_size = 0.2, random_state = 0)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "ipykernel_launcher:4: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\nipykernel_launcher:7: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n"
     ]
    }
   ],
   "source": [
    "# 2 对数据进行分批\n",
    "from torch.utils.data import Dataset, DataLoader, TensorDataset\n",
    "\n",
    "torch_dataset = TensorDataset(torch.tensor(train_features),torch.tensor(train_labels))\n",
    "data_loader = DataLoader(dataset=torch_dataset,batch_size=256,shuffle=True)\n",
    "\n",
    "test_features = torch.tensor(test_features)\n",
    "test_labels = torch.tensor(test_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3 定义神经网络\n",
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "\n",
    "class MLP(nn.Module):\n",
    "    \"\"\"A simple implementation of Deep Neural Network model\"\"\"\n",
    "    def __init__(self):\n",
    "        super(MLP, self).__init__()\n",
    "        self.hidden = 300\n",
    "        self.model = nn.Sequential(\n",
    "            nn.Linear(470, self.hidden),\n",
    "            #nn.Dropout(0.5),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(self.hidden, self.hidden),\n",
    "            #nn.Dropout(0.5),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(self.hidden, 5))\n",
    "    def forward(self, x):\n",
    "        return self.model(x)\n",
    "\n",
    "model = MLP().to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义优化器损失函数\n",
    "optimizer = torch.optim.Adam(params=model.parameters(), lr=0.0001)\n",
    "criterion = nn.CrossEntropyLoss()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "第0次迭代,acc:0.4056034482758621\n",
      "第1次迭代,acc:0.39741379310344827\n",
      "第2次迭代,acc:0.4271551724137931\n",
      "第3次迭代,acc:0.4844827586206897\n",
      "第4次迭代,acc:0.55\n",
      "第5次迭代,acc:0.5758620689655173\n",
      "第6次迭代,acc:0.5922413793103448\n",
      "第7次迭代,acc:0.6331896551724138\n",
      "第8次迭代,acc:0.6405172413793103\n",
      "第9次迭代,acc:0.6504310344827586\n",
      "第10次迭代,acc:0.6655172413793103\n",
      "第11次迭代,acc:0.665948275862069\n",
      "第12次迭代,acc:0.6844827586206896\n",
      "第13次迭代,acc:0.7043103448275863\n",
      "第14次迭代,acc:0.709051724137931\n",
      "第15次迭代,acc:0.7125\n",
      "第16次迭代,acc:0.7314655172413793\n",
      "第17次迭代,acc:0.715948275862069\n",
      "第18次迭代,acc:0.7258620689655172\n",
      "第19次迭代,acc:0.7443965517241379\n",
      "第20次迭代,acc:0.756896551724138\n",
      "第21次迭代,acc:0.7538793103448276\n",
      "第22次迭代,acc:0.7629310344827587\n",
      "第23次迭代,acc:0.7607758620689655\n",
      "第24次迭代,acc:0.7689655172413793\n",
      "第25次迭代,acc:0.7418103448275862\n",
      "第26次迭代,acc:0.7745689655172414\n",
      "第27次迭代,acc:0.7668103448275863\n",
      "第28次迭代,acc:0.7728448275862069\n",
      "第29次迭代,acc:0.7775862068965518\n",
      "第30次迭代,acc:0.7879310344827586\n",
      "第31次迭代,acc:0.7793103448275862\n",
      "第32次迭代,acc:0.7801724137931034\n",
      "第33次迭代,acc:0.7831896551724138\n",
      "第34次迭代,acc:0.7849137931034482\n",
      "第35次迭代,acc:0.781896551724138\n",
      "第36次迭代,acc:0.7844827586206896\n",
      "第37次迭代,acc:0.7952586206896551\n",
      "第38次迭代,acc:0.7922413793103448\n",
      "第39次迭代,acc:0.7939655172413793\n",
      "第40次迭代,acc:0.8004310344827587\n",
      "第41次迭代,acc:0.803448275862069\n",
      "第42次迭代,acc:0.7961206896551725\n",
      "第43次迭代,acc:0.8\n",
      "第44次迭代,acc:0.8021551724137931\n",
      "第45次迭代,acc:0.8099137931034482\n",
      "第46次迭代,acc:0.8172413793103448\n",
      "第47次迭代,acc:0.8112068965517242\n",
      "第48次迭代,acc:0.8142241379310344\n",
      "第49次迭代,acc:0.8116379310344828\n",
      "第50次迭代,acc:0.8094827586206896\n",
      "第51次迭代,acc:0.8185344827586207\n",
      "第52次迭代,acc:0.8211206896551724\n",
      "第53次迭代,acc:0.8202586206896552\n",
      "第54次迭代,acc:0.8185344827586207\n",
      "第55次迭代,acc:0.8206896551724138\n",
      "第56次迭代,acc:0.8232758620689655\n",
      "第57次迭代,acc:0.8219827586206897\n",
      "第58次迭代,acc:0.8262931034482759\n",
      "第59次迭代,acc:0.8275862068965517\n",
      "第60次迭代,acc:0.8267241379310345\n",
      "第61次迭代,acc:0.8232758620689655\n",
      "第62次迭代,acc:0.8288793103448275\n",
      "第63次迭代,acc:0.8267241379310345\n",
      "第64次迭代,acc:0.8288793103448275\n",
      "第65次迭代,acc:0.8262931034482759\n",
      "第66次迭代,acc:0.8284482758620689\n",
      "第67次迭代,acc:0.8362068965517241\n",
      "第68次迭代,acc:0.8293103448275863\n",
      "第69次迭代,acc:0.8357758620689655\n",
      "第70次迭代,acc:0.8306034482758621\n",
      "第71次迭代,acc:0.8448275862068966\n",
      "第72次迭代,acc:0.8301724137931035\n",
      "第73次迭代,acc:0.8357758620689655\n",
      "第74次迭代,acc:0.8344827586206897\n",
      "第75次迭代,acc:0.8327586206896552\n",
      "第76次迭代,acc:0.8422413793103448\n",
      "第77次迭代,acc:0.843103448275862\n",
      "第78次迭代,acc:0.8336206896551724\n",
      "第79次迭代,acc:0.8383620689655172\n",
      "第80次迭代,acc:0.8318965517241379\n",
      "第81次迭代,acc:0.8482758620689655\n",
      "第82次迭代,acc:0.8349137931034483\n",
      "第83次迭代,acc:0.8413793103448276\n",
      "第84次迭代,acc:0.846551724137931\n",
      "第85次迭代,acc:0.8439655172413794\n",
      "第86次迭代,acc:0.8422413793103448\n",
      "第87次迭代,acc:0.840948275862069\n",
      "第88次迭代,acc:0.8474137931034482\n",
      "第89次迭代,acc:0.8521551724137931\n",
      "第90次迭代,acc:0.8512931034482759\n",
      "第91次迭代,acc:0.8543103448275862\n",
      "第92次迭代,acc:0.85\n",
      "第93次迭代,acc:0.8495689655172414\n",
      "第94次迭代,acc:0.8538793103448276\n",
      "第95次迭代,acc:0.8504310344827586\n",
      "第96次迭代,acc:0.8564655172413793\n",
      "第97次迭代,acc:0.853448275862069\n",
      "第98次迭代,acc:0.8521551724137931\n",
      "第99次迭代,acc:0.8538793103448276\n",
      "第100次迭代,acc:0.8590517241379311\n",
      "第101次迭代,acc:0.8564655172413793\n",
      "第102次迭代,acc:0.8568965517241379\n",
      "第103次迭代,acc:0.8577586206896551\n",
      "第104次迭代,acc:0.8594827586206897\n",
      "第105次迭代,acc:0.8586206896551725\n",
      "第106次迭代,acc:0.8620689655172413\n",
      "第107次迭代,acc:0.8594827586206897\n",
      "第108次迭代,acc:0.8603448275862069\n",
      "第109次迭代,acc:0.8711206896551724\n",
      "第110次迭代,acc:0.8603448275862069\n",
      "第111次迭代,acc:0.8655172413793103\n",
      "第112次迭代,acc:0.8637931034482759\n",
      "第113次迭代,acc:0.8625\n",
      "第114次迭代,acc:0.8629310344827587\n",
      "第115次迭代,acc:0.8741379310344828\n",
      "第116次迭代,acc:0.8607758620689655\n",
      "第117次迭代,acc:0.8676724137931034\n",
      "第118次迭代,acc:0.8573275862068965\n",
      "第119次迭代,acc:0.8672413793103448\n",
      "第120次迭代,acc:0.868103448275862\n",
      "第121次迭代,acc:0.8672413793103448\n",
      "第122次迭代,acc:0.8637931034482759\n",
      "第123次迭代,acc:0.865948275862069\n",
      "第124次迭代,acc:0.8737068965517242\n",
      "第125次迭代,acc:0.8698275862068966\n",
      "第126次迭代,acc:0.8732758620689656\n",
      "第127次迭代,acc:0.8702586206896552\n",
      "第128次迭代,acc:0.8728448275862069\n",
      "第129次迭代,acc:0.8719827586206896\n",
      "第130次迭代,acc:0.8732758620689656\n",
      "第131次迭代,acc:0.8689655172413793\n",
      "第132次迭代,acc:0.8728448275862069\n",
      "第133次迭代,acc:0.8719827586206896\n",
      "第134次迭代,acc:0.8719827586206896\n",
      "第135次迭代,acc:0.8758620689655172\n",
      "第136次迭代,acc:0.8754310344827586\n",
      "第137次迭代,acc:0.8741379310344828\n",
      "第138次迭代,acc:0.8741379310344828\n",
      "第139次迭代,acc:0.8711206896551724\n",
      "第140次迭代,acc:0.871551724137931\n",
      "第141次迭代,acc:0.871551724137931\n",
      "第142次迭代,acc:0.8737068965517242\n",
      "第143次迭代,acc:0.8732758620689656\n",
      "第144次迭代,acc:0.8724137931034482\n",
      "第145次迭代,acc:0.875\n",
      "第146次迭代,acc:0.8745689655172414\n",
      "第147次迭代,acc:0.8814655172413793\n",
      "第148次迭代,acc:0.8702586206896552\n",
      "第149次迭代,acc:0.8711206896551724\n",
      "第150次迭代,acc:0.8762931034482758\n",
      "第151次迭代,acc:0.8728448275862069\n",
      "第152次迭代,acc:0.878448275862069\n",
      "第153次迭代,acc:0.8754310344827586\n",
      "第154次迭代,acc:0.8775862068965518\n",
      "第155次迭代,acc:0.8698275862068966\n",
      "第156次迭代,acc:0.8810344827586207\n",
      "第157次迭代,acc:0.8758620689655172\n",
      "第158次迭代,acc:0.8797413793103448\n",
      "第159次迭代,acc:0.8771551724137931\n",
      "第160次迭代,acc:0.8762931034482758\n",
      "第161次迭代,acc:0.8767241379310344\n",
      "第162次迭代,acc:0.8780172413793104\n",
      "第163次迭代,acc:0.8771551724137931\n",
      "第164次迭代,acc:0.8862068965517241\n",
      "第165次迭代,acc:0.8870689655172413\n",
      "第166次迭代,acc:0.8875\n",
      "第167次迭代,acc:0.8775862068965518\n",
      "第168次迭代,acc:0.8775862068965518\n",
      "第169次迭代,acc:0.8780172413793104\n",
      "第170次迭代,acc:0.878448275862069\n",
      "第171次迭代,acc:0.8831896551724138\n",
      "第172次迭代,acc:0.8797413793103448\n",
      "第173次迭代,acc:0.8793103448275862\n",
      "第174次迭代,acc:0.8827586206896552\n",
      "第175次迭代,acc:0.8780172413793104\n",
      "第176次迭代,acc:0.8875\n",
      "第177次迭代,acc:0.8771551724137931\n",
      "第178次迭代,acc:0.8853448275862069\n",
      "第179次迭代,acc:0.8823275862068966\n",
      "第180次迭代,acc:0.8788793103448276\n",
      "第181次迭代,acc:0.8810344827586207\n",
      "第182次迭代,acc:0.8875\n",
      "第183次迭代,acc:0.8853448275862069\n",
      "第184次迭代,acc:0.8814655172413793\n",
      "第185次迭代,acc:0.8853448275862069\n",
      "第186次迭代,acc:0.8827586206896552\n",
      "第187次迭代,acc:0.8844827586206897\n",
      "第188次迭代,acc:0.8827586206896552\n",
      "第189次迭代,acc:0.8857758620689655\n",
      "第190次迭代,acc:0.8866379310344827\n",
      "第191次迭代,acc:0.8849137931034483\n",
      "第192次迭代,acc:0.884051724137931\n",
      "第193次迭代,acc:0.878448275862069\n",
      "第194次迭代,acc:0.8862068965517241\n",
      "第195次迭代,acc:0.8831896551724138\n",
      "第196次迭代,acc:0.8836206896551724\n",
      "第197次迭代,acc:0.8857758620689655\n",
      "第198次迭代,acc:0.8892241379310345\n",
      "第199次迭代,acc:0.8823275862068966\n"
     ]
    }
   ],
   "source": [
    "# 4 训练模型\n",
    "for e in range(200):\n",
    "    # 训练模式\n",
    "    model.train()\n",
    "    for batch_x, batch_y in data_loader:\n",
    "        #print(\"batch_x\",batch_x)\n",
    "        batch_x, batch_y = batch_x.to(device), batch_y.to(device)\n",
    "        pred_y = model(batch_x)\n",
    "        #print(\"pred_y\",pred_y)\n",
    "        loss = criterion(pred_y, batch_y.long()) / len(data_loader)\n",
    "        loss.backward()\n",
    "        # bound l2 sensitivity (gradient clipping)          \n",
    "        optimizer.step()\n",
    "        optimizer.zero_grad()\n",
    "    \n",
    "    # 测试模式\n",
    "    model.eval()\n",
    "    correct = 0\n",
    "    total_sample = 0\n",
    "    for i in range(1):\n",
    "        t_pred_y = model(test_features)\n",
    "        # print(t_pred_y)\n",
    "        _, predicted = torch.max(t_pred_y, 1)\n",
    "        predicted = predicted\n",
    "        # print(predicted.dtype,test_labels.dtype)\n",
    "        correct += (predicted == test_labels).sum().item()\n",
    "        total_sample += test_labels.size(0)\n",
    "    acc = correct / total_sample\n",
    "\n",
    "    print(\"第{}次迭代,acc:{}\".format(e,acc))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}